package com.cheyun.netsalev3.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.SwipeRefreshTrigger;
import com.aspsine.swipetoloadlayout.SwipeTrigger;
import com.cheyun.netsalev3.R;


public class RefreshHeaderView extends RelativeLayout implements SwipeTrigger, SwipeRefreshTrigger {

    private ImageView ivRefresh;
    private TextView txtRefresh;

    private AnimationDrawable mAnimDrawable;


    private int mTriggerOffset;


    public RefreshHeaderView(Context context) {
        super(context);
    }

    public RefreshHeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTriggerOffset = context.getResources().getDimensionPixelOffset(R.dimen.refresh_header_height);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ivRefresh = (ImageView) findViewById(R.id.ivRefresh);
        txtRefresh = (TextView) findViewById(R.id.txtRefresh);
        mAnimDrawable = (AnimationDrawable) ivRefresh.getBackground();

    }

    @Override
    public void onRefresh() {
        if (!mAnimDrawable.isRunning()) {
            mAnimDrawable.start();
        }
        txtRefresh.setText("正在加载数据");
    }

    @Override
    public void onPrepare() {

    }

    @Override
    public void onMove(int y, boolean isComplete, boolean automatic) {
        if (!isComplete) {
            if (y >= getHeight()) {
                txtRefresh.setText("松开立即刷新");
            } else {
                txtRefresh.setText("下拉可以刷新");
                if (!mAnimDrawable.isRunning()) {
                    mAnimDrawable.start();
                }
            }
        } else {
            txtRefresh.setText("正在加载数据");
        }
    }

    @Override
    public void onRelease() {
        if (!mAnimDrawable.isRunning()) {
            mAnimDrawable.start();
        }
    }

    @Override
    public void onComplete() {
    }

    @Override
    public void onReset() {
        mAnimDrawable.stop();
    }
}
